package bar.dark.lee.common;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import bar.dark.lee.R;
import bar.dark.lee.setting.category.CategoryInActivity;
import bar.dark.lee.setting.category.CategoryMoveActivity;
import bar.dark.lee.setting.category.CategoryOutActivity;
import bar.dark.lee.setting.category.CategoryTabActivity;

public class RadioDialogActivity extends ListActivity
{
	public static final String RADIOSTRING = "radio_string";
	public static final String RADIODATA = "radio_data";
	public static final String RADIOSELECT = "radio_select";
	public static final String RADIOTYPE = "radio_type";
	public static final String RADIOBUTTONSTRING = "radio_button_string";
	
	public static final String CATEGORY_IN_TYPE = "category_in_type";
	public static final String CATEGORY_IN_TYPE_STRING = "수입 카테고리 추가";
	public static final String CATEGORY_OUT_TYPE = "category_out_type";
	public static final String CATEGORY_OUT_TYPE_STRING = "지출 카테고리 추가";
	public static final String CATEGORY_MOVE_TYPE = "category_move_type";
	public static final String CATEGORY_MOVE_TYPE_STRING = "이동 카테고리 추가";
	
	public static final int REQUEST_CATEGORY_IN_ADD = 111;
	public static final int REQUEST_CATEGORY_OUT_ADD = 222;
	
	public static final int REFRESH_CATEGORY = 1010;
	
	public static final int REQUEST_BANK_CARD = 333;
	
	ArrayList<String> list_;
	String select_;
	String radioType;
	String radioButtonStr;
	
	private Button button;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.radio_group_activity);
		
		Intent intent = getIntent();
		list_ = intent.getStringArrayListExtra(RADIODATA);
		select_ = intent.getStringExtra(RADIOSELECT);
		radioType = intent.getStringExtra(RADIOTYPE);
		radioButtonStr = intent.getStringExtra(RADIOBUTTONSTRING);
		
		button = (Button) findViewById(R.id.radioGroupButton);
		
		if (select_ == null)
		{
			select_ = "";
		}
		
		if (list_ == null)
		{
			return;
		}
		
		if (radioType != null && radioButtonStr != null)
		{
			if (radioType.equalsIgnoreCase(CATEGORY_IN_TYPE))
			{
				button.setText(radioButtonStr);
				button.setVisibility(View.VISIBLE);
				
				button.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(RadioDialogActivity.this, CategoryInActivity.class);
						intent.putExtra(CategoryInActivity.TITLE_VISIBLE, true);
						startActivityForResult(intent, REQUEST_CATEGORY_IN_ADD);
					}
				});
			}
			else if (radioType.equalsIgnoreCase(CATEGORY_OUT_TYPE))
			{
				button.setText(radioButtonStr);
				button.setVisibility(View.VISIBLE);
				
				button.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(RadioDialogActivity.this, CategoryOutActivity.class);
						intent.putExtra(CategoryOutActivity.TITLE_VISIBLE, true);
						startActivityForResult(intent, REQUEST_CATEGORY_OUT_ADD);
					}
				});
			}
			else if (radioType.equalsIgnoreCase(CATEGORY_MOVE_TYPE))
			{
				button.setText(radioButtonStr);
				button.setVisibility(View.VISIBLE);
				
				button.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(RadioDialogActivity.this, CategoryMoveActivity.class);
						intent.putExtra(CategoryOutActivity.TITLE_VISIBLE, true);
						startActivityForResult(intent, REQUEST_CATEGORY_OUT_ADD);
					}
				});
			}
		}
		
		getListView().setFooterDividersEnabled(true);
		
		RadioDialogAdapter adapter = new RadioDialogAdapter(this, R.layout.radio_list_item_dialog, list_);
		setListAdapter(adapter);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if (requestCode == REQUEST_CATEGORY_IN_ADD || requestCode == REQUEST_CATEGORY_OUT_ADD)
		{
			if (resultCode == REFRESH_CATEGORY)
			{
				Intent intent = new Intent();
				setResult(REFRESH_CATEGORY, intent);
				finish();
			}
		}
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		super.onListItemClick(l, v, position, id);
		
		for (int i = 0; i < l.getChildCount(); i++)
		{
			ViewGroup vg = (ViewGroup) l.getChildAt(i);
			ViewGroup vgc = (ViewGroup) vg.getChildAt(0);
			RadioButton r = (RadioButton) vgc.getChildAt(1);
			r.setChecked(false);
		}
		
		RadioButton rb = (RadioButton) v.findViewById(R.id.radioButton);
		rb.setChecked(true);
		Intent intent = new Intent();
		intent.putExtra(RADIOSTRING, list_.get(position));
		setResult(Activity.RESULT_OK, intent);
		finish();
	}
	
	public class RadioDialogAdapter extends ArrayAdapter<String>
	{
		ArrayList<String> list_;
		
		public RadioDialogAdapter(Context context, int textViewResourceId,
				ArrayList<String> list)
		{
			super(context, textViewResourceId, list);
			this.list_ = list;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View v = convertView;
			
			if (v == null)
			{
				LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v  = li.inflate(R.layout.radio_list_item_dialog, null);
			}
			
			String data = list_.get(position);
			
			TextView tv = (TextView) v.findViewById(R.id.radioText);
			RadioButton rb = (RadioButton) v.findViewById(R.id.radioButton);
			
			if (data.trim().equals(select_))
			{
				rb.setChecked(true);
			}
			else
			{
				rb.setChecked(false);
			}
			
			tv.setText(data);
			
			return v;
		}
	}
}
